<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="联系人名称" prop="name">
                    <el-input class="w-[280px]" v-model="queryParams.name" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button
                    v-perms="['linkman:add']"
                    type="primary"
                    @click="handleAdd()"
                    :disabled="disabled"
                >
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="ID" prop="id" min-width="50" />
                <el-table-column label="联系人名称" prop="name" min-width="130" />
                <el-table-column label="工作邮箱" prop="workEmail" min-width="130" />
                <el-table-column label="职位" prop="position" min-width="130" />
                <el-table-column label="私人邮箱" prop="email" min-width="130" />
                <el-table-column label="是否在职" prop="isOnJob" min-width="130" />
                <el-table-column label="称呼" prop="callName" min-width="130" />
                <el-table-column label="性别" prop="sex" min-width="130" />
                <el-table-column label="联系人类别" prop="type" min-width="130" />
                <el-table-column label="座机" prop="number" min-width="130" />
                <el-table-column label="电话" prop="phone" min-width="130" />
                <el-table-column label="创建时间" prop="createTime" min-width="180" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['linkman:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                            :disabled="disabled"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['linkman:del']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                            :disabled="disabled"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="linkman">
import { linkmanDelete, linkmanLists } from '@/api/linkman'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './addLinkManEdit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const route = useRoute()
const router = useRouter()
const disabled = ref(true)
const queryParams = reactive({
    name: '',
    systemType: '1',
    threadId: route.query.id
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: linkmanLists,
    params: queryParams
})

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await linkmanDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

const disabledAll = async () => {
    disabled.value = false
}

route.query.id && disabledAll()
getLists()
</script>
